package com.yn.xgame;

import com.yn.xgame.message.*;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 在同一vm内，可以用函数式方式做线程间交互, 便捷方法 Created by wangboo on 2017/7/11.
 */
public interface IFuncServerLike extends IServer {

	/**
	 * 在该进程上执行一个函数
	 *
	 * @param callback
	 *            执行函数
	 */
	default void applyFunc0(Callback callback) {
		pushMessage(new Func0Message(callback));
	}

	default <T1, T2> void applyFunc2(T1 t1, T2 t2, IFunc2<T1, T2> func2) {
		pushMessage(new Func2Message<>(t1, t2, func2));
	}

	default <T1, T2, T3> void applyFunc3(T1 t1, T2 t2, T3 t3, IFunc3<T1, T2, T3> func3) {
		pushMessage(new Func3Message<>(t1, t2, t3, func3));
	}

	/**
	 * 在该进程上用参数t，执行一个带参数的函数
	 *
	 * @param t
	 *            参数
	 * @param consumer
	 *            执行函数
	 * @param <T>
	 *            参数类型
	 */
	default <T> void applyFunc1(T t, Consumer<T> consumer) {
		pushMessage(new Func1Message<>(t, consumer));
	}

	default <R> void applySupplyAndConsume(IServer consumeServer, Supplier<R> supplier, Consumer<R> consumer) {
		pushMessage(new SupplyAndConsumeMessage<>(consumeServer, supplier, consumer));
	}

	default <T, R> void applySupplyAndSingleConsume1(IServer consumeServer, T t, Function<T, R> func,
			Consumer<R> consumer) {
		pushMessage(new SupplyAndSingleConsumeMessage1<>(consumeServer, t, func, consumer));
	}

	default <T, R> void applySupplyAndConsume1(IServer consumeServer, T t, Function<T, R> func, IFunc2<T, R> consumer) {
		pushMessage(new SupplyAndConsumeMessage1<>(consumeServer, t, func, consumer));
	}

	default <T1, T2, R> void applySupplyAndConsume2(IServer consumeServer, T1 t1, T2 t2, IRFunc2<T1, T2, R> iRFunc,
			IFunc2<T1, R> consumer) {
		pushMessage(new SupplyAndConsumeMessage2<>(consumeServer, t1, t2, iRFunc, consumer));
	}
}
